/*Copyright 2009-2010 by Ward, David J ( pymethius@ominian.net )
Licensed work, all rights reserved.*/
Ext.namespace("wm");
Ext.namespace("wmlib.data");


/**
 *WM's implementation of the ExtJS.data.Proxy
 *Assumptions:
 *  Following the pattern emerging on the server side, model's are exposed
 *  as a method of a handler.
 *
 *
 */
wmlib.data.WMProxy = function(handler, proxyAction){
    
    
    /**
     *Prior to Proxy.doRequest being called, a sanity check is made: this.api[action] !== undefined && this.load !== undefined
     *will throw up an exception.  So... we need to populate this.api out with quasi-useful values.
     *     
     *
     */
    var api = {};
    for (var i in Ext.data.Api.actions){
        //crud are inferred 
        api[i] = handler + "." + proxyAction ;
    }
    wmlib.data.WMProxy.superclass.constructor.call(this, { api:api });
    var self = this;
    self.rpcHandler = handler;
    self.proxyAction = proxyAction;

};

Ext.extend(wmlib.data.WMProxy, Ext.data.DataProxy, {

    /**
     *TODO - once this has been hashed, abstract/compact it back down
     */
    doRead: function(rs, params, reader, callback, scope, arg){
        //console.debug(arguments.callee, arguments.callee.argumentNames().zip(arguments));
        console.log("doRead called");
        wm.router.makeRequest(this.api["read"], {query:'read'}, function(response){
            console.log("read response being handled");
            var state = false;
            var result;
            try{
                result = reader.readRecords(response);
                state = true;
            }catch(err){
                state = false;
                callback.call(scope, null, arg, false)
                return;
            }
            //Success callback
            callback.call(scope, result, arg, true);
            
            
        });
    },
    /**
     *The API documentation goes into length about the different parameters here, so I won't be repeating myself.
     *Instead the best way to visualize what's going on, doRequest is like a visitor pattern in that encapsulates
     *and abstracts data.api.actions away.
     *
     *Regardless of success or failure, it must be
     *
     */
    doRequest: function(action, rs, params, reader, callback, scope, arg){        
        //console.debug(arguments.callee, arguments.callee.argumentNames().zip(arguments));
        console.log("doRequest called");
        var proxyHandler = Ext.emptyFn;
        switch(action){
            case "create":
                proxyHandler = this.doCreate.bind(this);                
                break;
            case "read":
                proxyHandler = this.doRead.bind(this);
                break;
            case "update":
                proxyHandler = this.doUpdate.bind(this);
                break;
            case "delete":
                proxyHandler = this.doDelete.bind(this);
                break;
            default:
                throw "Unknown " + action + ", unable to handle";
        }
        try{
            proxyHandler( rs, params, reader, callback, scope, arg);
        }catch(err){
            this.fireEvent("loadexception", this, arg, null, err);
            console.debug(err);
        }
    }    
    
});